/*
 * [TestJScrollPane.java]
 *
 * Summary: demonstrate the use of javax.swing.JScrollPane.
 *
 * Copyright: (c) 2011-2013 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.7+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2011-09-20 initial version
 */
package gui;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import java.awt.Container;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

/**
 * Ventana de ayuda
 * 
 * @author lgonzalez
 * @version 1.0 15-05-2013
 * @since 15-05-2013
 *
 */
public final class Gelp  
    {
	
	/**
     * is user busy with the scroller?
     */
    @SuppressWarnings("unused")
	private static boolean userBusyScrolling;
    private JFrame jFrame;
    private static Logger __log = LogManager.getLogger(Gelp.class);

    // --------------------------- main() method ---------------------------

    /**
     * Debugging harness for a Frame
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
    	new Gelp(true);
        }
    public Gelp (){
    	
    }
    
        /**
         * fire up a JFrame on the Swing thread
         */
        public Gelp(Boolean pruebas)
            {
            jFrame = new JFrame();
            jFrame.setBounds(100, 100, 500, 400);
            final Container contentPane = jFrame.getContentPane();
            
            // The user hitting enter inserts a \n character into the text.
            
            
            contentPane.add( inicializar()/* not jEditorPane! */ );
            //jFrame.setSize( 270, 120 );
            //jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            jFrame.validate();
            jFrame.setVisible( true );
            
            }
            
        public JScrollPane inicializar(){
            final JEditorPane jEditorPane = new JEditorPane();

            jEditorPane.setContentType( "text/html" );
            jEditorPane.setEditable( false );

            jEditorPane.setText( this.leerAyuda());
            
            jEditorPane.setCaretPosition(0);
            // add what you want to scroll, e.g. a JTextPane, JTextArea or JPanel
            // other option is VERTICAL_SCROLLBAR_NEVER to turn off bar.
            final JScrollPane scroller = new JScrollPane( jEditorPane,
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );

            // control the speed effect of the wheelmouse
            scroller.getVerticalScrollBar().setUnitIncrement( 16 );
            scroller.getVerticalScrollBar().addAdjustmentListener( new AdjustmentListener()
            {
            /**
             * detect user fiddling with the scroller
             */
            public void adjustmentValueChanged( AdjustmentEvent e )
                {
                userBusyScrolling = e.getValueIsAdjusting();
                }
            } );

            // GOTCHA! does nothing. Must wait until rendering complete.
            scroller.getVerticalScrollBar().setValue( 0 );

            // Using the default VERTICAL_SCROLLBAR_AS_NEEDED will use scrollbars even when
            // not strictly needed.  If you want precise control you must decide for yourself
            // dynamically.
            scroller.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
            
            return scroller;
            }
        
        private String leerAyuda() {
    		File archivo = null;
    		FileReader fr = null;
    		BufferedReader br = null;
    		StringBuffer miBuff= new StringBuffer();

    		try {
    			// Apertura del fichero y creacion de BufferedReader para poder
    			// hacer una lectura comoda (disponer del metodo readLine()).
    			archivo = new File("./res/Ayuda.html");    			
    			fr = new FileReader(archivo);
    			br = new BufferedReader(fr);

    			// Lectura del fichero
    			String linea;
    			while ((linea = br.readLine()) != null)
    				miBuff.append(linea);
    		} catch (Exception e) {
    			__log.error(e);
    			JOptionPane
    			.showMessageDialog(jFrame,
    					"Error leyendo el archivo de ayuda no se ha encontrado "+archivo.getAbsolutePath());
    		} finally {
    			// En el finally cerramos el fichero, para asegurarnos
    			// que se cierra tanto si todo va bien como si salta
    			// una excepcion.
    			try {
    				if (null != fr) {
    					fr.close();
    				}
    			} catch (Exception e2) {
    				__log.error(e2);
    				JOptionPane
    				.showMessageDialog(jFrame,
    						"Error leyendo el archivo de ayuda");
    			}
    		}
    		return miBuff.toString();
    	}
    
    }